<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="/js/vue.js"></script>
</head>
<body>
    <div id="app">
        <table>
            <tr>
                <th>姓名</th>
                <th>年龄</th>
            </tr>
            <tr v-for="(item,index) in list">
                <th>{{item.name}}</th>
                <th>{{item.age}}</th>
            </tr>
        </table>
        <p>总年龄：{{totalAge}}</p>

        姓：<input type="text" v-model="firstName"> +
    名：<input type="text" v-model="lastName"> =
    <span>{{fullName}}</span><br><br> 
    <button @click="rename">改名卡</button>
    </div>
</body>
<script>
    const app = new Vue({
        el:'#app',
        data:{
            list:[{'name':'张三','age':'18'},
            {'name':'李四','age':'28'},
            {'name':'王五','age':'38'}],    
            firstName: '刘',
            lastName: '备'
        },
        methods:{
            rename(){
                this.fullName = '张三';
            }
        },
        computed:{
            totalAge(){
                let sum = 0;
                for(let i=0;i<this.list.length;i++){
                    // console.log(this.list[i]);
                    sum += parseInt(this.list[i].age);
                }
                return sum;
            },
            fullName: {
                get(){
                    return this.firstName + this.lastName;
                },
                set(value){
                    this.firstName = value.substr(0,1);
                    this.lastName = value.substr(1);
                }
            }
        }
    })
</script>
</html>